//
//  TestView.m
//  移动
//
//  Created by lanou3g on 16/4/17.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "RMAddImageView.h"

@interface RMAddImageView (){
    BOOL _isFinish;
}
@property (nonatomic, strong) UIImage *phImage;
@property (nonatomic, strong) UIImage *btImage;
@property(nonatomic,strong)UIImageView * photoImageView;
@property(nonatomic,strong)UIButton * deleteButton;
@end

@implementation RMAddImageView

-(instancetype)initWithFrame:(CGRect)frame withPhotoImage:(UIImage *)phImage andBtnImage:(UIImage *)btImage{

    if (self = [super initWithFrame:frame]) {
        [self setUpView];
        self.phImage = phImage;
        self.btImage = btImage;
    }
    return self;
}

- (void)layoutSubviews{
    self.photoImageView.image = self.phImage;

}

-(void)setUpView{
    


    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self addGestureRecognizer:panGesture];

    //mark!!!:长按手势
    UILongPressGestureRecognizer * longDelete = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longDeleteAction:)];
    [self addGestureRecognizer:longDelete];
    
    
    
    //mark!!!:点击手势
    UITapGestureRecognizer * tapImage = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tapImage];
    self.backgroundColor = [UIColor whiteColor];
    


    self.photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, self.bounds.size.width - 10, self.bounds.size.height- 10)];

    [self addSubview:self.photoImageView];
    self.userInteractionEnabled = YES;



}


-(void)panView:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint offsetPoint = [gesture translationInView:gesture.view];
        [gesture setTranslation:CGPointZero inView:gesture.view];
//        self.frame.origin.y += offsetPoint.y;
//        float y = self.frame.origin.y + offsetPoint.y;
//        self.frame = CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height);
        
        if ((gesture.view.frame.origin.x + gesture.view.frame.size.width  + offsetPoint.x< [UIScreen mainScreen].bounds.size.width-1)&& (gesture.view.frame.origin.x  + offsetPoint.x > 1) && (gesture.view.frame.origin.y + offsetPoint.y > 1)){
            gesture.view.transform = CGAffineTransformTranslate(gesture.view.transform, offsetPoint.x, offsetPoint.y);
            [gesture setTranslation:CGPointZero inView:gesture.view];
        }
        
        CGFloat x = gesture.view.frame.origin.x - 1;
        CGFloat y = gesture.view.frame.origin.y - 1;
        CGFloat width = gesture.view.frame.size.width + 2;
        CGFloat height = gesture.view.frame.size.height + 2;
        if (x<0) {
            x = 0;
            width = gesture.view.frame.size.width + 1;
        }
        
        if (y<0) {
            y= 0;
            height = gesture.view.frame.size.height + 1;
        }
        
        
        CGRect rect1 = CGRectMake(x, y, width, height);
        
        self.updateFrame(gesture.view.frame);
        
//        self.updateFrame();
        

        //设置占位
        //先取出该图像在所在下标
//        NSInteger index = [_imageViewArray indexOfObject:gesture.view];
        
        //用现在的替换上一次状态
//        [_exclusionPathsArray replaceObjectAtIndex:index withObject:[self translatedBzierPath:gesture.view.frame]];
//        self.textContainer.exclusionPaths = _exclusionPathsArray;
    }
}


//mark!!!:长按删除
-(void)longDeleteAction:(UILongPressGestureRecognizer *)longGesture{
    if (self.deleteButton) {
        return;
    }
    if (longGesture.state != UIGestureRecognizerStateBegan) {
        return;
     }
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.deleteButton.frame = CGRectMake(0, 0, 25, 25);
    self.deleteButton.alpha = 0;
    self.deleteButton.backgroundColor  = [UIColor whiteColor];
    [self.deleteButton setImage:self.btImage forState:(UIControlStateNormal)];
    [self addSubview:self.deleteButton];
    self.deleteButton.layer.cornerRadius = self.deleteButton.frame.size.width/2 + 0;
    self.deleteButton.layer.masksToBounds = YES;
    self.deleteButton.imageEdgeInsets = UIEdgeInsetsMake(-1, -1, -1, -1);
    [UIView animateWithDuration:0.3 animations:^{
        self.deleteButton.alpha = 1;
         NSLog(@"%@--------",        [NSThread currentThread]);
    } completion:^(BOOL finished) {
        NSLog(@"%@",        [NSThread currentThread]);
    }];
   [self.deleteButton addTarget:self action:@selector(deleteViewAction) forControlEvents:(UIControlEventTouchUpInside)];
    
}

//mark!!!:删除事件
-(void)deleteViewAction{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        self.photoImageView.frame = CGRectMake(0, 0 , 25,25);
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 25, 25);
        CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotation.toValue = @(M_PI*1.5);
        [self.deleteButton.layer addAnimation:rotation forKey:nil];
    } completion:^(BOOL finished) {
        [self.deleteButton removeFromSuperview];
        [self.photoImageView removeFromSuperview];
        self.deleteButton = nil;
        self.photoImageView = nil;
        self.phImage = nil;
        self.btImage = nil;
        [self removeFromSuperview];
        self.updateFrame(CGRectZero);

    }];
}

- (void)dealloc{
  
}

//mark!!!:点击事件
-(void)tapAction{

    if (!self.deleteButton) {
        return;
    }
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3, 3, 1)];
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.toValue  = [NSNumber numberWithFloat:0.0f];
    
    CAAnimationGroup *animationgroup = [CAAnimationGroup animation];
    animationgroup.animations = [NSArray arrayWithObjects:scaleAnimation, opacityAnimation, nil];
    
    animationgroup.duration = 0.7;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    animationgroup.delegate = self;
    [self.deleteButton.layer addAnimation:animationgroup forKey:nil];

    


}

//mark!!!:移动
//-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UITouch * touch = [touches anyObject];
//    CGPoint fromPoint = [touch previousLocationInView:self];
//    CGPoint toPoint = [touch locationInView:self];
//#warning 设置移动范围
//    CGPoint centerPoint = self.center;
//    self.center = CGPointMake(centerPoint.x, centerPoint.y + toPoint.y - fromPoint.y);
//
//}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.deleteButton removeFromSuperview];
    self.deleteButton = nil;
}



@end
